<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket 客户端</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: #fff;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: rgba(0, 0, 0, 0.7);
            border-radius: 10px;
            padding: 25px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
        }
        
        header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            background: linear-gradient(to right, #fdbb2d, #b21f1f);
            -webkit-background-clip: text;
            background-clip: text;
            color: transparent;
        }
        
        .status {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-bottom: 20px;
            font-size: 1.2rem;
        }
        
        .status-indicator {
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: #ff4757;
        }
        
        .status.connected .status-indicator {
            background-color: #2ed573;
            animation: pulse 1.5s infinite;
        }
        
        @keyframes pulse {
            0% { transform: scale(1); opacity: 1; }
            50% { transform: scale(1.2); opacity: 0.7; }
            100% { transform: scale(1); opacity: 1; }
        }
        
        .control-panel {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        button {
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-weight: bold;
            transition: all 0.3s ease;
            flex: 1;
        }
        
        #connectBtn {
            background-color: #2ed573;
            color: white;
        }
        
        #connectBtn:hover {
            background-color: #26c965;
        }
        
        #disconnectBtn {
            background-color: #ff4757;
            color: white;
        }
        
        #disconnectBtn:hover {
            background-color: #ff3742;
        }
        
        #clearBtn {
            background-color: #5352ed;
            color: white;
        }
        
        #clearBtn:hover {
            background-color: #4241d6;
        }
        
        .message-input {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        input {
            flex: 1;
            padding: 12px 15px;
            border: none;
            border-radius: 5px;
            background-color: rgba(255, 255, 255, 0.1);
            color: white;
            font-size: 1rem;
        }
        
        input:focus {
            outline: 2px solid #5352ed;
        }
        
        #sendBtn {
            background-color: #5352ed;
            color: white;
        }
        
        #sendBtn:hover {
            background-color: #4241d6;
        }
        
        .logs {
            background-color: rgba(0, 0, 0, 0.5);
            border-radius: 5px;
            padding: 15px;
            height: 300px;
            overflow-y: auto;
            font-family: monospace;
            font-size: 0.9rem;
            margin-top: 20px;
        }
        
        .log-entry {
            margin-bottom: 8px;
            padding-bottom: 8px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }
        
        .log-time {
            color: #fdbb2d;
        }
        
        .log-sent {
            color: #2ed573;
        }
        
        .log-received {
            color: #5352ed;
        }
        
        .log-error {
            color: #ff4757;
        }
        
        .log-info {
            color: #70a1ff;
        }
        
        footer {
            text-align: center;
            margin-top: 30px;
            font-size: 0.9rem;
            color: rgba(255, 255, 255, 0.6);
        }
        
        @media (max-width: 600px) {
            .control-panel, .message-input {
                flex-direction: column;
            }
            
            h1 {
                font-size: 1.8rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>WebSocket 客户端</h1>
            <p>与 Go WebSocket 服务器通信的测试页面</p>
        </header>
        
        <div class="status">
            <div class="status-indicator"></div>
            <span id="statusText">未连接</span>
        </div>
        
        <div class="control-panel">
            <button id="connectBtn">连接服务器</button>
            <button id="disconnectBtn" disabled>断开连接</button>
            <button id="clearBtn">清空日志</button>
        </div>
        
        <div class="message-input">
            <input type="text" id="messageInput" placeholder="输入要发送的消息..." disabled>
            <button id="sendBtn" disabled>发送消息</button>
        </div>
        
        <div class="logs" id="logContainer">
            <!-- 日志将在这里显示 -->
        </div>
        
        <footer>
            <p>基于 Go WebSocket 服务器 | 每个连接都有唯一 ID</p>
        </footer>
    </div>

    <script>
        // 全局变量
        let socket = null;
        let connectionId = null;
        
        // DOM 元素
        const connectBtn = document.getElementById('connectBtn');
        const disconnectBtn = document.getElementById('disconnectBtn');
        const clearBtn = document.getElementById('clearBtn');
        const sendBtn = document.getElementById('sendBtn');
        const messageInput = document.getElementById('messageInput');
        const statusText = document.getElementById('statusText');
        const statusIndicator = document.querySelector('.status');
        const logContainer = document.getElementById('logContainer');
        
        // 添加日志
        function addLog(message, type = 'info') {
            const logEntry = document.createElement('div');
            logEntry.className = `log-entry log-${type}`;
            
            const timestamp = new Date().toLocaleTimeString();
            logEntry.innerHTML = `<span class="log-time">[${timestamp}]</span> ${message}`;
            
            logContainer.appendChild(logEntry);
            logContainer.scrollTop = logContainer.scrollHeight;
        }
        
        // 连接 WebSocket 服务器
        function connect() {
            // 根据你的服务器地址修改
            const wsUrl = 'ws://localhost:9091/ws';
            //  const wsUrl = 'ws://114.55.132.50:9091/ws';
            
            try {
                socket = new WebSocket(wsUrl);
                
                socket.onopen = function(event) {
                    statusText.textContent = '已连接';
                    statusIndicator.classList.add('connected');
                    connectBtn.disabled = true;
                    disconnectBtn.disabled = false;
                    sendBtn.disabled = false;
                    messageInput.disabled = false;
                    
                    addLog('WebSocket 连接已建立', 'info');
                };
                
                socket.onmessage = function(event) {
                    addLog(`收到服务器消息: ${event.data}`, 'received');
                };
                
                socket.onclose = function(event) {
                    statusText.textContent = '未连接';
                    statusIndicator.classList.remove('connected');
                    connectBtn.disabled = false;
                    disconnectBtn.disabled = true;
                    sendBtn.disabled = true;
                    messageInput.disabled = true;
                    
                    if (event.wasClean) {
                        addLog(`连接已关闭，代码: ${event.code}, 原因: ${event.reason}`, 'info');
                    } else {
                        addLog('连接异常断开', 'error');
                    }
                };
                
                socket.onerror = function(error) {
                    addLog('WebSocket 错误发生', 'error');
                    console.error('WebSocket 错误:', error);
                };
                
            } catch (error) {
                addLog(`连接失败: ${error.message}`, 'error');
                console.error('连接失败:', error);
            }
        }
        
        // 断开连接
        function disconnect() {
            if (socket) {
                socket.close();
                socket = null;
            }
        }
        
        // 发送消息
        function sendMessage() {
            const message = messageInput.value.trim();
            if (!message) {
                alert('请输入要发送的消息');
                return;
            }
            
            if (socket && socket.readyState === WebSocket.OPEN) {
                socket.send(message);
                addLog(`发送消息: ${message}`, 'sent');
                messageInput.value = '';
            } else {
                addLog('WebSocket 未连接，无法发送消息', 'error');
            }
        }
        
        // 清空日志
        function clearLogs() {
            logContainer.innerHTML = '';
            addLog('日志已清空', 'info');
        }
        
        // 事件监听
        connectBtn.addEventListener('click', connect);
        disconnectBtn.addEventListener('click', disconnect);
        sendBtn.addEventListener('click', sendMessage);
        clearBtn.addEventListener('click', clearLogs);
        
        // 按回车发送消息
        messageInput.addEventListener('keypress', function(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        });
        
        // 初始日志
        addLog('页面已加载，点击"连接服务器"按钮开始测试', 'info');
    </script>
</body>
</html>